package orange.com.openstack.sahara.api;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import net.minidev.json.JSONObject;
import orange.com.openstack.sahara.data.Binaries;
import orange.com.openstack.sahara.httpConnection.RestCall;

import com.google.gson.Gson;
import com.jayway.jsonpath.JsonPath;

/**
 * class allowing the interaction with the JobBinaries Rest service
 * @author osvaldo.cocucci@gmail.com
 * 
 */
public class JobBinaries {
	String endpoint;
	String tenantId;
	String token;
	Gson gson;

	/**
	 * @param endpoint
	 * @param tenantId
	 * @param token
	 * 
	 *            constructor
	 */
	public JobBinaries(String endpoint, String tenantId, String token) {
		gson = new Gson();
		this.endpoint = endpoint;
		this.tenantId = tenantId;
		this.token = token;
	}

	/**
	 * 
	 * This operation returns the list of all created Job Binaries. This
	 * operation does not require a request body.
	 */
	public List<Binaries> listJobBinaries() {
		List<Binaries> binaries = new ArrayList<Binaries>();
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/job-binaries", null, "GET", token);
		try {
			if (connection.getResponseCode() != 200)
				return null;
			String JSONreponse = caller.getResponse(connection);
			connection.disconnect();
			List<JSONObject> binariesJSON = JsonPath.read(JSONreponse,
					"$.binaries[*]");

			for (JSONObject json : binariesJSON) {
				binaries.add(gson.fromJson(json.toJSONString(), Binaries.class));
			}

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return binaries;
	}

	/**
	 * @param dataSourceId
	 *            This operation shows information about a specified Job Binary.
	 *            This operation does not require a request body.
	 */
	public Binaries showJobBinaries(String jobBinariesId) {
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/job-binaries/" + jobBinariesId, null, "GET", token);
		try {
			if (connection.getResponseCode() != 200)
				return null;
			connection.connect();
			String JSONreponse = caller.getResponse(connection);
			connection.disconnect();
			return gson.fromJson(((JSONObject) JsonPath.read(JSONreponse,
					"$.job_binary")).toJSONString(), Binaries.class);

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param desc
	 * @param url
	 *            e.g "swift://container/jar-example.jar"
	 * @param password
	 * @param user
	 * @param type
	 *            can be "swift"
	 * @param name
	 *            can be "jar-example.jar"
	 * 
	 *            This operation shows information about the created Job Binary.
	 * @return the id of the binarie
	 */
	public Binaries createJobBinaries(String desc, String url, String password,
			String user, String type, String name) {
		RestCall caller = new RestCall();
		String payload = "{\"url\": \"" + url + "\",\"name\": \"" + name
				+ "\",\"description\": \"" + desc
				+ "\",\"extra\": {\"password\": \"" + password
				+ "\",\"user\": \"" + user + "\"}}";
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/job-binaries", payload, "POST", token);
		try {
			if (connection.getResponseCode() != 202)
				return null;
			String JSONreponse = caller.getResponse(connection);
			connection.disconnect();
			return gson.fromJson(((JSONObject) JsonPath.read(JSONreponse,
					"$.job_binary")).toJSONString(), Binaries.class);

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param dataSourceId
	 *            Removes Job Binary object This operation returns nothing. This
	 *            operation does not require a request body.
	 */
	public boolean deleteJobBinaries(String jobBinariesId) {
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/job-binaries/" + jobBinariesId, null, "DELETE", token);
		try {
			if (connection.getResponseCode() == 204) {
				connection.disconnect();
				return true;
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		connection.disconnect();
		return false;
	}

	/**
	 * @param dataSourceId
	 * 
	 *            show the data source detail according to the id
	 */
	public void getJobBinariesData(String jobBinariesId) {
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/job-binaries/" + jobBinariesId + "/data", null, "GET",
				token);
		try {
			connection.connect();
			System.out.println(caller.getResponse(connection));
			connection.disconnect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param jobBinariesName
	 * @return
	 */
	public boolean exist(String jobBinariesName) {
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/job-binaries", null, "GET", token);
		try {
			connection.connect();

			String JSONreponse = caller.getResponse(connection);
			List<Object> binaries = JsonPath.read(JSONreponse,
					"$.binaries[?(@.name == '" + jobBinariesName + "')]");
			connection.disconnect();
			return binaries.size() > 0;
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * get the id of a job
	 * 
	 * @param jobBinariesName
	 * @return
	 */
	public String getId(String jobBinariesName) {
		RestCall caller = new RestCall();
		HttpURLConnection connection = caller.connect(endpoint + tenantId
				+ "/job-binaries", null, "GET", token);
		try {
			connection.connect();

			String JSONreponse = caller.getResponse(connection);
			List<Object> binarie = JsonPath.read(JSONreponse,
					"$.binaries[?(@.name == '" + jobBinariesName + "')].id");
			connection.disconnect();
			return (String) binarie.get(0);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}
